package android.pot.com.smartcar.action;

import android.content.Context;
import android.content.Intent;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.api.Api;
import android.pot.com.smartcar.base.MyResponseHandler;
import android.pot.com.smartcar.config.MyConfig;
import android.pot.com.smartcar.tool.ACache;
import android.pot.com.smartcar.tool.LoadingTool;
import android.pot.com.smartcar.tool.LogTool;
import android.pot.com.smartcar.tool.TipToast;
import android.pot.com.smartcar.ui.login.LoginsActivity;

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import myokhttp.MyOkHttp;
import myokhttp.response.JsonResponseHandler;

public class Action {

    private JSONObject mCacheResponse;
    private MyOkHttp client;
    private static Action instance;

    public Action() {
        client = MyOkHttp.get();
    }

    /**
     * 获取句柄
     *
     * @return
     */
    public static Action get() {
        instance = new Action();

        return instance;
    }

    /**
     * @param context
     * @param url
     * @param params
     * @param addSsid
     * @param iResponseHandler
     * @param needCache
     */
    public void postRequest(final Context context, final String url, final Map<String, String> params, boolean addSsid, final MyResponseHandler iResponseHandler, final boolean needCache) {

        if (needCache) {
            ACache aCache = ACache.get(context);
            if (aCache != null) {
                mCacheResponse = aCache.getAsJSONObject(url);
            }
            if (mCacheResponse != null) {
                String message = null;
                try {
                    message = mCacheResponse.getString("msg");
                } catch (JSONException e) {
                }
                try {
                    iResponseHandler.onSuccess(message, mCacheResponse.getJSONObject("data"));
                } catch (JSONException e) {
                    iResponseHandler.onSuccess(message, mCacheResponse);
                }
            }
        }
        client.post(context, url, params, addSsid, new JsonResponseHandler() {
            @Override
            public void onSuccess(int code, String message, JSONObject response) {
                LogTool.print("post_jsonObject", "url:" + url + "  response:" + response.toString());
                if (code == 200) {
                    if (mCacheResponse == null) {//无缓存数据
                        try {
                            iResponseHandler.onSuccess(message, response.getJSONObject("data"));
                        } catch (JSONException e) {
                            iResponseHandler.onSuccess(message, response);
                        }
                        if (needCache) {
                            ACache aCache = ACache.get(context);
                            aCache.put(url, response);
                        }
                    } else {
                        JsonElement cache = new JsonPrimitive(mCacheResponse.toString());
                        JsonElement net = new JsonPrimitive(response.toString());
                        if (!cache.equals(net)) {
                            try {
                                iResponseHandler.onSuccess(message, response.getJSONObject("data"));
                            } catch (JSONException e) {
                                iResponseHandler.onSuccess(message, response);
                            }
                            if (needCache) {
                                ACache aCache = ACache.get(context);
                                aCache.put(url, response);
                            }
                        }
                    }
                } else if (code == 1111) {
                    TipToast.show(message);
                    LoadingTool.EndLoading(context);
                    Intent intent = new Intent(context, LoginsActivity.class);
                    context.startActivity(intent);
                } else if (code == 400) {
                    TipToast.show(context.getString(R.string.login_time_out));
                    LoadingTool.EndLoading(context);
                    Intent intent = new Intent(context, LoginsActivity.class);
                    context.startActivity(intent);
                }
                else if(code == 201){//暂无数据
                    try {
                        iResponseHandler.onSuccess(message, response.getJSONObject("data"));
                    } catch (JSONException e) {
                        iResponseHandler.onSuccess(message, response);
                    }
                }
                else {
                    TipToast.show(message);
                    LoadingTool.EndLoading(context);
                    iResponseHandler.onFailure(code, message);
                }
            }

            @Override
            public void onFailure(int statusCode, String error_msg) {
                LogTool.print("post_failure", statusCode + "：" + url + ":" + error_msg);
                iResponseHandler.onFailure(statusCode, error_msg);
                LoadingTool.EndLoading(context);
                if (statusCode == 0) {
                    TipToast.show(error_msg);
                } else {
                    TipToast.show(error_msg);
                }
            }
        });
    }


    /**
     * 登录
     */
    public void Login(final Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.login, params, false, iResponseHandler, false);
    }

    /**
     * 注册
     */
    public void register(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.register, params, false, iResponseHandler, false);
    }
    /**
     * 注册协议列表
     */
    public void protocol_list(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.protocol_list, params, false, iResponseHandler, false);
    }
    /**
     * 注册协议详情
     */
    public void protocol_detail(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.protocol_detail, params, false, iResponseHandler, false);
    }
    /**
     * 忘记密码
     */
    public void forget_password(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.forget_password, params, false, iResponseHandler, false);
    }
    /**
     * 发送注册验证码
     */
    public void sendCode(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.send_code, params, false, iResponseHandler, false);
    }

    /**
     *  首页
     */
    public void home(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.home, params, false, iResponseHandler, false);
    }
    /**
     *  购买商品
     */
    public void buy_goods(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.buy_goods, params, true, iResponseHandler, false);
    }
    /**
     *  商品详情
     */
    public void goods_details(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.goods_details, params, true, iResponseHandler, false);
    }
    /**
     *  广告列表
     */
    public void ad_list(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.ad_list, params, true, iResponseHandler, false);
    }

    /**
     *  发现
     */
    public void find_img(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.find_img, params, true, iResponseHandler, false);
    }
    /**
     * 退出登录
     */
    public void memberLogout(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.memberLogout, params, true, iResponseHandler, false);
    }

    /**
     *  生成订单
     */
    public void generate_orders(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.generate_orders, params, true, iResponseHandler, false);
    }
    /**
     *  支付
     */
    public void pay(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.pay, params, true, iResponseHandler, false);
    }
    /**
     *  个人中心
     */
    public void personal_center(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.personal_center, params, true, iResponseHandler, false);
    }
    /**
     *  我的订单
     */
    public void my_order(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.my_order, params, true, iResponseHandler, false);
    }
    /**
     *  收藏列表
     */
    public void collection_list(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.collection_list, params, true, iResponseHandler, false);
    }
    /**
     *  浏览记录
     */
    public void browse_record(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.browse_record, params, true, iResponseHandler, false);
    }
    /**
     *  平台入驻
     */
    public void platform_stationed(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.platform_stationed, params, true, iResponseHandler, false);
    }
    /**
     *  平台入驻分类
     */
    public void platform_stationed_class(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.platform_stationed_class, params, true, iResponseHandler, false);
    }
    /**
     *  修改密码
     */
    public void change_password(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.change_password, params, true, iResponseHandler, false);
    }
    /**
     *  联系我们
     */
    public void contact_us(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.contact_us, params, true, iResponseHandler, false);
    }
    /**
     *  区域
     */
    public void area_list(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.area_list, params, false, iResponseHandler, false);
    }
    /**
     *  店铺列表
     */
    public void shop_list(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.shop_list, params, true, iResponseHandler, false);
    }
    /**
     *  店铺详情
     */
    public void shop_details(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.shop_details, params, true, iResponseHandler, false);
    }
    /**
     *  收藏
     */
    public void collection(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.collection, params, true, iResponseHandler, false);
    }
    /**
     *  分销商
     */
    public void distributor(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.distributor, params, true, iResponseHandler, false);
    }
    /**
     *  客户订单
     */
    public void d_order(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.d_order, params, true, iResponseHandler, false);
    }
    /**
     *  客户列表
     */
    public void customer_list(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.customer_list, params, true, iResponseHandler, false);
    }
    /**
     *  提现
     */
    public void withdraw(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.withdraw, params, true, iResponseHandler, false);
    }
    /**
     *  提现记录
     */
    public void withdraw_list(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.withdraw_list, params, true, iResponseHandler, false);
    }
    /**
     *  启动页广告
     */
    public void start_page_ad(Context context, Map<String, String> params, MyResponseHandler iResponseHandler) {
        postRequest(context, MyConfig.Url + Api.start_page_ad, params, false, iResponseHandler, false);
    }
}
